package logic

import (
	"context"
	"fmt"
	"goods/goodsrpc/goodsrpc"
	"goods/goodsrpc/internal/svc"
	"goods/models/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectAddLogic) CollectAdd(in *goodsrpc.CollectAddRequest) (*goodsrpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line

	var goods mysql.Goods
	mysql.DB.Debug().Where("id=?", in.GoodsId).Find(&goods)
	if goods.ID == 0 {
		return nil, fmt.Errorf("找不到该商品")
	}
	collect := mysql.Collect{
		GoodsId: in.GoodsId,
		UserId:  in.UserId,
	}
	mysql.DB.Debug().Create(&collect)
	goods.CollectNum = goods.CollectNum + 1
	mysql.DB.Debug().Updates(&goods)
	return &goodsrpc.CollectAddResponse{
		CollectId: int64(collect.ID),
	}, nil
}
